package demo2;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Test {
    public static void func() throws ClassNotFoundException,
            NoSuchMethodException, InvocationTargetException,
            InstantiationException, IllegalAccessException {

        Class<?> classReflect = Class.forName("demo2.TestEnum");
        //注意对应的参数，获得对应的构造方法来构造对象，当前枚举类是提供了两个参数分别是String和int
        Constructor<?> constructor = classReflect.getDeclaredConstructor(String.class, int.class, int.class, String.class);
        constructor.setAccessible(true);

        TestEnum testEnum = (TestEnum) constructor.newInstance("父类参数", 888, 666, "子类参数");
        System.out.println("获得枚举私有构造方法：" + testEnum);
    }

    public static void main(String[] args) throws ClassNotFoundException,
            NoSuchMethodException, InvocationTargetException,
            InstantiationException, IllegalAccessException {

        func();
    }

    public enum TestEnum {
        INSTANCE;
        public TestEnum getInstance(){
            return INSTANCE;
        }
        public static void main(String[] args) {
            TestEnum singleton1=TestEnum.INSTANCE;
            TestEnum singleton2=TestEnum.INSTANCE;
            System.out.println("两个实例是否相同："+(singleton1==singleton2));
        }
    }

}
